<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Golden Stars Animation</title>
    <style>
        body { margin: 0; }
        canvas { display: block; }
    </style>
</head>
<body>
<canvas id="starsCanvas"></canvas>

<script>
    const canvas = document.getElementById('starsCanvas');
    const ctx = canvas.getContext('2d');

    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    let stars = [];

    function init() {
        for (let i = 0; i < 25; i++) {
            let x = Math.random() * canvas.width;
            let y = Math.random() * canvas.height;
            let r = Math.random() * 2 + 1; // 半径
            let vx = Math.random() * 3 - 1.5; // 水平方向速度
            let vy = Math.random() * 3 - 1.5; // 垂直方向速度
            let opacity = 1; // 初始不透明度
            let decay = Math.random() * 0.002 + 0.001; // 不透明度衰减速率
            stars.push({x, y, r, vx, vy, opacity, decay});
        }
    }

    function draw() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = 'rgba(0, 0, 0, 1)';
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        stars.forEach(star => {
            ctx.beginPath();
            ctx.arc(star.x, star.y, star.r, 0, Math.PI * 2);
            ctx.fillStyle = `rgba(255, 215, 0, ${star.opacity})`; // 金色
            ctx.fill();
        });
    }

    function update() {
        stars.forEach(star => {
            star.x += star.vx;
            star.y += star.vy;
            star.opacity -= star.decay;
            if (star.opacity <= 0) {
                stars.splice(stars.indexOf(star), 1);
            } else {
                // 限制星星在一定范围内移动
                if (star.x < 0 || star.x > canvas.width) star.vx *= -1;
                if (star.y < 0 || star.y > canvas.height) star.vy *= -1;
            }
        });

        // 如果星星数量减少到一半，再添加一些新的星星
        if (stars.length < 20) {
            let x = Math.random() * canvas.width;
            let y = Math.random() * canvas.height;
            let r = Math.random() * 2 + 1;
            let vx = Math.random() * 3 - 1.5;
            let vy = Math.random() * 3 - 1.5;
            let opacity = 1;
            let decay = Math.random() * 0.002 + 0.001;
            stars.push({x, y, r, vx, vy, opacity, decay});
        }
    }

    function animate() {
        requestAnimationFrame(animate);
        update();
        draw();
    }

    init();
    animate();

    window.addEventListener('resize', () => {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        init();
    });
</script>
</body>
</html>